package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.user.WithdrawalDetailVO;
import com.cruise.game.model.vo.user.WithdrawalVO;
import com.cruise.game.service.admin.AdminWithdrawalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Map;

/**
 * 管理员提现控制器
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员提现相关接口")
@RestController
@RequestMapping("/api/admin/withdrawals")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminWithdrawalController extends BaseController {

    @Resource
    private AdminWithdrawalService adminWithdrawalService;

    /**
     * 获取提现申请列表
     */
    @ApiOperation("获取提现申请列表")
    @GetMapping
    public ApiResult<PageResult<WithdrawalVO>> getWithdrawalList(
            @ApiParam(value = "申请状态", required = false) @RequestParam(value = "status", required = false) String status,

            @ApiParam(value = "开始日期", required = false) @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,

            @ApiParam(value = "结束日期", required = false) @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,

            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(adminWithdrawalService.getWithdrawalList(status, startDate, endDate, page, pageSize));
    }

    /**
     * 获取提现申请详情
     */
    @ApiOperation("获取提现申请详情")
    @GetMapping("/{withdrawalId}")
    public ApiResult<WithdrawalDetailVO> getWithdrawalDetail(
            @ApiParam(value = "提现申请ID", required = true) @PathVariable("withdrawalId") Long withdrawalId) {
        return ApiResult.success(adminWithdrawalService.getWithdrawalDetail(withdrawalId));
    }

    /**
     * 处理提现申请
     */
    @ApiOperation("处理提现申请")
    @PostMapping("/{withdrawalId}/process")
    public ApiResult<Boolean> processWithdrawal(
            @ApiParam(value = "提现申请ID", required = true) @PathVariable("withdrawalId") Long withdrawalId,

            @ApiParam(value = "是否批准", required = true) @RequestParam("approved") Boolean approved,

            @ApiParam(value = "处理备注", required = false) @RequestParam(value = "remark", required = false) String remark) {
        return ApiResult.success("提现申请已处理",
                adminWithdrawalService.processWithdrawal(withdrawalId, approved, remark));
    }

    /**
     * 批量处理提现申请
     */
    @ApiOperation("批量处理提现申请")
    @PostMapping("/batch-process")
    public ApiResult<Map<String, Object>> batchProcessWithdrawal(
            @ApiParam(value = "提现申请ID列表", required = true) @RequestParam("withdrawalIds") String withdrawalIds,

            @ApiParam(value = "是否批准", required = true) @RequestParam("approved") Boolean approved,

            @ApiParam(value = "处理备注", required = false) @RequestParam(value = "remark", required = false) String remark) {

        Map<String, Object> result = adminWithdrawalService.batchProcessWithdrawal(withdrawalIds, approved, remark);

        int successCount = (int) result.get("successCount");
        int failCount = (int) result.get("failCount");
        int total = (int) result.get("total");

        // 根据处理结果返回不同的消息
        if (successCount == total) {
            return ApiResult.success("批量处理成功", result);
        } else if (successCount > 0 && failCount > 0) {
            return ApiResult.success("部分处理成功，请查看详细信息", result);
        } else {
            // 全部失败的情况已经在服务层抛出异常，这里不需要处理
            return ApiResult.success("批量处理结果", result);
        }
    }

    /**
     * 查看当日提现统计
     */
    @ApiOperation("查看当日提现统计")
    @GetMapping("/today-statistics")
    public ApiResult<Map<String, Object>> getTodayStatistics() {
        return ApiResult.success(adminWithdrawalService.getTodayWithdrawalStatistics());
    }
}